<template>
  <div class="property-list">
    <div class="section-header">
      <h3 class="section-title">周边房源推荐</h3>
      <span class="more-text" @click="viewMore">更多推荐 ></span>
    </div>
    
    <div class="property-grid">
      <div
        v-for="property in properties"
        :key="property.id"
        class="property-item"
        @click="handlePropertyClick(property)"
      >
        <div class="property-image">
          <img :src="property.image" :alt="property.title" />
          <div class="property-tag">{{ property.tag }}</div>
        </div>
        <div class="property-info">
          <div class="location-info">
            <img src="@/assets/imgs/location.svg" alt="位置" class="location-icon" />
            <span class="location-text">{{ property.location }}</span>
          </div>
          <h4 class="property-title">{{ property.title }}</h4>
          <div class="property-details">
            <span class="price">¥{{ property.price }}/月</span>
            <span class="area">{{ property.area }}㎡</span>
          </div>
          <div class="property-actions">
            <button class="action-btn call-btn">电话</button>
            <button class="action-btn visit-btn" @click.stop="handleVisitClick(property)">实地看</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { getRoomRecommendListApi } from '@/api/room'

const router = useRouter()

// 房源数据
const properties = ref([])
const loading = ref(false)

// 获取房源推荐数据
const fetchProperties = async () => {
  try {
    loading.value = true
    const data = await getRoomRecommendListApi()
    
    // 转换数据格式以适配现有组件
    properties.value = data.data.map(item => ({
      id: item.id,
      title: item.houseTitle,
      location: item.address,
      price: item.rentPriceListing,
      area: item.rentArea,
      image: item.housePicture,
      tag: item.tags && item.tags.length > 0 ? item.tags[0].name : '整租'
    }))
  } catch (error) {
    console.error('获取房源推荐数据失败:', error)
    ElMessage.error('获取房源推荐数据失败')
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取数据
onMounted(() => {
  fetchProperties()
})

const handlePropertyClick = (property) => {
  console.log('房源点击:', property)
  router.push({ name: 'RentRoomDetail', params: { id: property.id } })
}

// 处理实地看按钮点击
const handleVisitClick = (property) => {
  console.log('实地看按钮点击:', property)
  router.push({ name: 'RentRoomDetail', params: { id: property.id } })
}

const viewMore = () => {
  router.push('/See')
}
</script>

<style scoped>
.property-list {
  background-color: #fff;
  margin: 0 16px 16px;
  border-radius: 12px;
  padding: 20px 16px 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.more-text {
  font-size: 14px;
  color: #409eff;
  cursor: pointer;
}

.more-text:hover {
  text-decoration: underline;
}

.property-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.property-item {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
  background-color: #fff;
}

.property-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-color: #409eff;
}

.property-image {
  position: relative;
  height: 120px;
  overflow: hidden;
}

.property-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.property-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background-color: #409eff;
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.property-info {
  padding: 12px;
}

.location-info {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.location-icon {
  width: 12px;
  height: 12px;
  margin-right: 4px;
}

.location-text {
  font-size: 12px;
  color: #666;
}

.property-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0 0 8px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.property-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.price {
  font-size: 16px;
  font-weight: 600;
  color: #e74c3c;
}

.area {
  font-size: 12px;
  color: #666;
}

.property-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  flex: 1;
  padding: 6px 12px;
  border: 1px solid #409eff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.call-btn {
  background-color: #fff;
  color: #409eff;
}

.call-btn:hover {
  background-color: #409eff;
  color: #fff;
}

.visit-btn {
  background-color: #409eff;
  color: #fff;
}

.visit-btn:hover {
  background-color: #337ab7;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .property-list {
    margin: 0 12px 12px;
    padding: 16px 12px 12px;
  }
  
  .property-grid {
    gap: 8px;
  }
  
  .property-image {
    height: 100px;
  }
  
  .property-info {
    padding: 8px;
  }
  
  .property-title {
    font-size: 13px;
  }
  
  .price {
    font-size: 14px;
  }
}
</style>
